package com.maikec.utils.mvc;
/**
 * 邢帅教育
 * <p>
 * 本源代码由数据中心及其作者共同所有，未经版权持有者的事先书面授权，
 * 不得使用、复制、修改、合并、发布、分发和/或销售本源代码的副本。
 *
 * @copyright Copyright (c) 2018. [图片]xsteach.com all rights reserved.
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author maikec
 * @date 2018/7/21
 */
@Controller
public class HttpStatusErrorController implements ErrorController {

    private final NotFoundController notFoundController;
    private final ServerErrorController serverErrorController;
    @Autowired
    public HttpStatusErrorController(NotFoundController notFoundController, ServerErrorController serverErrorController) {
        this.notFoundController = notFoundController;
        this.serverErrorController = serverErrorController;
    }
    @RequestMapping("/error")
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
        if(HttpStatus.NOT_FOUND.value() == response.getStatus()) {
            return notFoundController.notFoundError();
        } else if(HttpStatus.INTERNAL_SERVER_ERROR.value() == response.getStatus()) {
            return serverErrorController.serverError();
        } else if (HttpStatus.METHOD_NOT_ALLOWED.value() == response.getStatus()) {
            System.out.println("405");
        }
       throw new UnsupportedOperationException();
    }

    @Override
    public String getErrorPath() {
        return null;
    }
}
